import Vue from 'vue'
import Router from 'vue-router'
import Login from './components/Login.vue'
import Home from './components/Home.vue'
import User from './components/users/User.vue'
import Welcome from './components/Welcome.vue'
import Right from './components/power/Right.vue'
import Role from './components/power/Role.vue'
import Cate from './components/goods/Cate.vue'
import Param from './components/goods/Param.vue'
import Goods from './components/goods/Goods.vue'
import AddGoods from './components/goods/AddGoods'
import Order from './components/order/Orders.vue'
import Reports from './components/report/Reports.vue'

Vue.use(Router)

const router = new Router({
    routes: [{
            path: '/login',
            component: Login
        },
        {
            path: '/',
            redirect: '/login'
        },

        {
            path: '/home',
            redirect: '/welcome',
            component: Home,
            children: [{
                path: '/users',
                component: User
            }, {
                path: '/welcome',
                component: Welcome
            }, {
                path: '/rights',
                component: Right
            }, {
                path: '/roles',
                component: Role
            }, {
                path: '/categories',
                component: Cate
            }, {
                path: '/params',
                component: Param
            }, {
                path: '/goods',
                component: Goods
            }, {
                path: '/goods/addgoods',
                component: AddGoods
            }, {
                path: '/orders',
                component: Order
            }, {
                path: '/reports',
                component: Reports
            }]
        }
    ]
})
router.beforeEach((to, from, next) => {
    if (to.path === '/login') return next()
    const toKen = window.sessionStorage.getItem('token')
    if (!toKen) return next('/login')
    next()
})

export default router